package com.teach.javafxclient.controller.base;

import com.teach.javafxclient.Globalvar;
import com.teach.javafxclient.util.HttpUtil;
import javafx.fxml.FXML;
import javafx.geometry.Rectangle2D;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.stage.FileChooser;
import kong.unirest.HttpResponse;
import kong.unirest.json.JSONObject;

import java.io.File;


/**
 * @author Liang Jianlin
 * @date 2024-05-16 01:19
 */
public class SettingsController {
    public VBox avatarUpload;
    @FXML
    public ImageView avatar;
    public Label outputLabel;
    public Button confirmbutton;
    public PasswordField pwd;
    public PasswordField pwdT;
    public Label pwdOutputLabel;
    public Button pwdConfirmbutton;
    public PasswordField prePwd;
    private File selectedFile;
    public void initialize(){
        Circle clip = new Circle(avatar.getFitWidth() / 2, avatar.getFitHeight() / 2, Math.min(avatar.getFitWidth() , avatar.getFitHeight()) / 2);
        avatar.setClip(clip);
        avatarUpload.setOnMouseClicked(e->{
            FileChooser fileChooser = new FileChooser();
            selectedFile = fileChooser.showOpenDialog(null);
            Image img = new Image(selectedFile.toURI().toString());
            double wid = Math.min(img.getWidth() , img.getHeight());
            Rectangle2D viewport = new Rectangle2D(img.getWidth()/2 - wid/2, img.getHeight()/2 - wid/2, wid, wid);
            avatar.setViewport(viewport);
            avatar.setImage(new Image(selectedFile.toURI().toString()));
        });
        //头像上传
        confirmbutton.setOnMouseClicked(e->{
            if(selectedFile == null){
                outputLabel.setText("未选择头像");
                return;
            }
            HttpResponse res = HttpUtil.post(Globalvar.baseUrl+"/api/users/"+ Globalvar.specificId +"/avatar")
                    .field("file",new File(String.valueOf(selectedFile)))
                    .asBytes();
            System.out.println(res.getStatus());
            if (res.getStatus() == 200){
                outputLabel.setText("修改成功");
            }else{
                outputLabel.setText("修改失败");
            }
        });
        pwdConfirmbutton.setOnAction(e->{
            if(prePwd.getText().isEmpty()){
                pwdOutputLabel.setText("初始密码不能为空");
                return;
            } else if (pwd.getText().isEmpty()) {
                pwdOutputLabel.setText("更改密码不能为空");
                return;
            } else if (!pwd.getText().equals(pwdT.getText())) {
                pwdOutputLabel.setText("两次输入密码不一致");
                return;
            }
            JSONObject data = new JSONObject();
            data.put("formalPassword",prePwd.getText());
            data.put("newPassword",pwd.getText());
            HttpResponse res = HttpUtil.post(Globalvar.baseUrl+"/api/users/password")
                    .header("Content-Type", "application/json")
                    .body(data)
                    .asJson();
            System.out.println(res.getStatus());
            if (res.getStatus() == 200){
                pwdOutputLabel.setText("修改成功");
            }else{
                pwdOutputLabel.setText("修改失败");
            }
        });
    }
}
